package com.ns.dcjh.listener;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.ns.dcjh.utils.BlockUtils;
import com.ns.dcjh.utils.ToastUtils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

public class MyUMAuthListener implements UMAuthListener {

    private Context mContext;
    private BlockUtils onSuccess;
    private BlockUtils onError;

    public MyUMAuthListener(Context context) {
        this.mContext = context;
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
        if (map == null) {
            ToastUtils.info(mContext, "获取用户信息失败");
            return;
        }
        if (this.onSuccess == null)   return;
        JSONObject data = (JSONObject) JSONObject.toJSON(map);
        this.onSuccess.callbackWithJSONObject(data);
    }

    @Override
    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
        if (this.onError == null)   return;
        JSONObject data = new JSONObject();
        data.put("action", i);
        this.onError.callbackWithJSONObject(data);
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media, int i) {
        ToastUtils.info(mContext, "您已取消登录");
    }

    public void setOnSuccess(BlockUtils onSuccess) {
        this.onSuccess = onSuccess;
    }

    public void setOnError(BlockUtils onError) {
        this.onError = onError;
    }
}
